package localdate;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateTimeDemo {

    public static void main(String[] args) {
        //获取LocalDateTime对象。
        LocalDateTime date = LocalDateTime.now();
        System.out.println(date);

        //获得当前日期对象的年、月、日、时、分、秒
        System.out.println( "当前年份：" + date.getYear() );
        System.out.println( "当前月份：" + date.getMonth() );
        System.out.println( "当前日份：" + date.getDayOfMonth() );
        System.out.println( "当前时：" + date.getHour() );
        System.out.println( "当前分：" + date.getMinute());
        System.out.println( "当前秒：" + date.getSecond());

        //根据指定日期，构造新的LocalDateTime日期对象
        date = LocalDateTime.of(2019, Month.DECEMBER, 12, 9, 21, 32);
        System.out.println(date.format(DateTimeFormatter.ISO_DATE));
        System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));
        System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss")));


        //LocalDateTime对象转Date对象
        Date d = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(d);

        //Date对象转LocalDateTime对象
        date = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println(date);
    }
}
